using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Knights.Recursos;
using System.Threading;

namespace Knights
{
    public class Juego : Game
    {
        private static GraphicsDeviceManager graphics;
        private SpriteBatch spriteBatch;

        private Rectangle tamanioVentana = new Rectangle(0, 0, 800, 500);
        private IPantalla pantallaActual;     

        public Juego()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            //graphics.IsFullScreen = true;
            graphics.PreferredBackBufferWidth = tamanioVentana.Width;
            graphics.PreferredBackBufferHeight = tamanioVentana.Height;
            graphics.ApplyChanges();

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(graphics.GraphicsDevice);

            // TODO: use this.Content to load your game content here
            Fuentes.CargarContenido(Content);
            Modelos.CargarContenido(Content);
            Sonidos.CargarContenido(Content);
            Texturas.CargarContenido(Content);

            CambiarPantalla(new PantallaMenu(this));
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        protected override void Update(GameTime gameTime)
        {
            // TODO: Add your update logic here
            pantallaActual.Actualizar();
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            // TODO: Add your drawing code here
            //spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise);
            spriteBatch.Begin();
            pantallaActual.Dibujar();
            spriteBatch.End();

            Thread.Sleep(15);
            base.Draw(gameTime);
        }

        public void CambiarPantalla(IPantalla pantalla)
        {
            pantallaActual = pantalla;
            pantallaActual.Inicializar();
        }

        public void DibujarTextura(Texture2D textura, Rectangle posicion, Color color)
        {
            spriteBatch.Draw(textura, posicion, color);
        }

        public void DibujarTexto(SpriteFont fuente, string texto, Vector2 posicion, Color color)
        {
            spriteBatch.DrawString(fuente, texto, posicion, color);
        }

        public Rectangle TamanioVentana { get { return tamanioVentana; } }

        public static GraphicsDevice Device { get { return graphics.GraphicsDevice; } }
    }
}
